/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package berlin.volders.rxdiff2;


import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.schedulers.Schedulers;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * ThingRepository.
 *
 * @since 2021-06-29
 */
public class ThingRepository {
    /**
     * 数据
     */
    public static final int COUNT = 100;
    /**
     * 动画时间
     */
    public static final int DURATION = 1000;
    /**
     * 一排几个
     */
    public static final int SEVEN = 7;
    /**
     * 截取数据范围
     */
    public static final float ZERO_POINT_EIGHT = 0.8f;
    private static final int CYCLE_COUNT = 10;
    private static final int SIXTY_FIVE = 65;
    private static final int TWENTY_FIVE = 25;
    private static SecureRandom random = new SecureRandom();

    private ThingRepository() {
    }

    /**
     * 初始化数据
     *
     * @param interval long
     * @param timeUnit TimeUnit
     * @return Flowable
     */
    public static Flowable<List<Thing>> latestThings(long interval, TimeUnit timeUnit) {
        return Flowable
                .interval(0, interval, timeUnit, Schedulers.computation())
                .map(i -> shuffle(randomThings()).subList(0, (int) (COUNT * ZERO_POINT_EIGHT)));
    }

    /**
     * 初始化数据
     *
     * @return List
     */
    public static List<Thing> randomThings() {
        List<Thing> things = new ArrayList<>(COUNT);
        for (int ii = 0; ii < COUNT; ii++) {
            things.add(newThing(ii));
        }
        return things;
    }

    /**
     * 移除数据
     *
     * @param things List
     * @return List
     */
    public static List<Thing> shuffle(List<Thing> things) {
        List<Thing> shuffled = new ArrayList<>(things.size());
        while (!things.isEmpty()) {
            Thing thing = things.remove(random.nextInt(things.size()));
            shuffled.add(thing);
        }
        return shuffled;
    }

    private static Thing newThing(int id) {
        Thing.Builder builder = Thing.builder();

        builder.id(id);
        char first = (char) (random.nextInt(TWENTY_FIVE) + SIXTY_FIVE);
        char second = (char) (random.nextInt(TWENTY_FIVE) + SIXTY_FIVE);
        char third = (char) (random.nextInt(TWENTY_FIVE) + SIXTY_FIVE);

        builder.text(String.valueOf(new char[]{first, second, third}));
        builder.color(random.nextInt());

        return builder.build();
    }

    /**
     * 初始化一个范围的值
     *
     * @return int
     */
    public static int randomCycleNumber() {
        return random.nextInt(CYCLE_COUNT) + CYCLE_COUNT;
    }

    /**
     * 初始化一个随机数
     *
     * @param count int
     * @return int
     */
    public static int randomlyGenerated(int count) {
        return random.nextInt(count);
    }
}
